/*
 * Platform.cpp
 * This file is part of FunBall 
 *
 * Copyright (C) 2012 - Francois Berder
 *
 * FunBall is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * FunBall is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 

#include <stdexcept>
#include <cassert>
#include "Platform.hpp"
#include "PhysicWorld.hpp"
#include "Ball.hpp"
#include "End.hpp"

Platform::Platform():
Entity(),
m_shape()
{

}
Platform::Platform(const Platform &p):
Entity(),
m_obj(p.m_obj),
m_shape(const_cast<Platform&>(p).m_shape)
{

}

void Platform::load(json::Object& o)
{
    json::String& dtype = o["type"];
    json::Number &posx = o["position"]["x"];
    json::Number &posy = o["position"]["y"];
    sf::Shape *shape = NULL;
    if(dtype.Value() == "circle")
    {
        json::Number &dradius = o["radius"];
        shape = new sf::CircleShape(dradius.Value());
        shape->setOrigin(dradius.Value(),dradius.Value());
    }
    else if(dtype.Value() == "square")
    {
        json::Number &dside = o["side"];
        shape = new sf::RectangleShape(sf::Vector2f(dside.Value(),dside.Value()));
        shape->setOrigin(dside.Value()/2.f,dside.Value()/2.f);
    }
    else if(dtype.Value() == "rectangle")
    {
        json::Number &dwidth = o["width"];
        json::Number &dheight = o["height"];
        shape = new sf::RectangleShape(sf::Vector2f(dwidth.Value(),dheight.Value()));
        shape->setOrigin(dwidth.Value()/2.f,dheight.Value()/2.f);    
    }
    else
        throw std::runtime_error("Invalid shape type while loading platform.");   
         
    json::Object d;
    d["type"] = json::String("static");
    d["angle"] = json::Number(0);
    d["position"]["x"] = json::Number(posx.Value());
    d["position"]["y"] = json::Number(-posy.Value());
    d["fixedRotation"] = json::Boolean(false);
    d["bullet"] = json::Boolean(false);
    d["shapeType"] = dtype;

    if(dtype.Value() == "circle")
        d["radius"] = o["radius"];
    else if(dtype.Value() == "square")
        d["fixtures"][0]["side"] = o["side"];
    
    else if(dtype.Value() == "rectangle")
    {
        d["width"] = o["width"];
        d["height"] = o["height"];
    }
    
    m_obj = CREATE_PHYSIC_OBJECT(d);
    m_obj.body()->SetUserData(this);
    
    shape->setPosition(posx.Value(),posy.Value());
    shape->setOutlineThickness(3.f);
    shape->setOutlineColor(sf::Color::Black);

    m_shape = std::auto_ptr<sf::Shape>(shape);
}

void Platform::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
    assert(m_shape.get() != NULL);
    target.draw(*m_shape.get(),states);
}

void Platform::collideWith(Entity& e)
{
    e.collideWith(*this);
}

void Platform::collideWith(Ball& b)
{

}

void Platform::collideWith(Platform& p)
{

}
void Platform::collideWith(End& e)
{

}

void Platform::endCollideWith(Entity& e)
{
    e.endCollideWith(*this);
}
void Platform::endCollideWith(Ball& b)
{
}
void Platform::endCollideWith(Platform& p)
{
}
void Platform::endCollideWith(End& e)
{

}
